package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.InspectTaskDetailMapper;
import cc.rengu.igas.bomp.common.entity.InspectTaskDetail;
import cc.rengu.igas.bomp.common.enums.InspectStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/5
 * Updated by zhangxuran on 2020/03/31
 */
public class InspectTaskDetailMapperImpl implements InspectTaskDetailMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertInspectTaskDetail(InspectTaskDetail inspectTaskDetail) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertInspectTaskDetail");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectTaskDetail.getInstId(), inspectTaskDetail.getInspectTaskNo(), inspectTaskDetail.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateInspectTaskDetailById(InspectTaskDetail inspectTaskDetail) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateInspectTaskDetailById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectTaskDetail.getInstId(), inspectTaskDetail.getInspectTaskNo(), inspectTaskDetail.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public InspectTaskDetail selectInspectTaskDetailById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskDetailById");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }

    @Override
    public InspectTaskDetail selectInspectTaskDetailByPrimaryKey(String instId, String inspectTaskNo, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskDetailByPrimaryKey");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setInspectTaskNo(inspectTaskNo);
        inspectTaskDetail.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, inspectTaskNo, mchntNo, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }

    @Override
    public List<InspectTaskDetail> selectInspectTaskDetailByManagerId(String instId, String managerId, String inspectBeginTime, String inspectEndTime) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskDetailByManagerId");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setManagerId(managerId);
        inspectTaskDetail.setInspectBeginTime(DateUtil.getDateByTimeStamp(DateUtil.getTimeStamp(inspectBeginTime, "yyyyMMdd"), "yyyy-MM-dd HH:mm:ss.SSS"));
        inspectTaskDetail.setInspectEndTime(DateUtil.getDateByTimeStamp(DateUtil.getTimeStamp(inspectEndTime, "yyyyMMdd"), "yyyy-MM-dd HH:mm:ss.SSS"));
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,managerId:<{}>,inspectBeginTime:<{}>,inspectEndTime:<{}>", instId, managerId, inspectBeginTime, inspectEndTime);
            return null;
        }
        return objectList.stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
    }

    @Override
    public List<InspectTaskDetail> selectInspectTaskDetailByInspectType(String instId, String inspectType, String inspectBeginTime, String inspectEndTime) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectTaskDetailByInspectType");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setInspectType(inspectType);
        inspectTaskDetail.setInspectBeginTime(DateUtil.getDateByTimeStamp(DateUtil.getTimeStamp(inspectBeginTime, "yyyyMMdd"), "yyyy-MM-dd HH:mm:ss.SSS"));
        inspectTaskDetail.setInspectEndTime(DateUtil.getDateByTimeStamp(DateUtil.getTimeStamp(inspectEndTime, "yyyyMMdd"), "yyyy-MM-dd HH:mm:ss.SSS"));
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectType:<{}>,inspectBeginTime:<{}>,inspectEndTime:<{}>", instId, inspectType, inspectType, inspectBeginTime, inspectEndTime);
            return null;
        }
        return objectList.stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
    }

    @Override
    public List<InspectTaskDetail> selectInspectTaskDetailByInspectTaskNo(String instId, String inspectTaskNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailByInspectTaskNo");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setInspectTaskNo(inspectTaskNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>", instId, inspectTaskNo);
            return null;
        }
        return objectList.stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectInspectTaskDetailByQueryFilter(InspectTaskDetail inspectTaskDetail, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailByInspectTaskNo");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>", inspectTaskDetail.getInstId(), inspectTaskDetail.getInspectTaskNo());
            return null;
        }
        return pageResult;
    }

    @Override
    public InspectTaskDetail selectBompInspectTaskDetailCountByTaskNo(String instId, String inspectTaskNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailCountByTaskNo");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setInspectTaskNo(inspectTaskNo);
        /*已巡检状态赋值*/
        inspectTaskDetail.setInspectStatus("10");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, inspectTaskNo, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }

    @Override
    public InspectTaskDetail selectBompInspectTaskDetailNeedCountByTaskNo(String instId, String inspectTaskNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailNeedCountByTaskNo");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setInspectTaskNo(inspectTaskNo);
        /*除已巡检状态外都是未巡检，赋值已巡检状态*/
        inspectTaskDetail.setInspectStatus(InspectStatusEnum.FINISH_STATUS.getInspectStatus());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,inspectTaskNo:<{}>,iReturnCode:<{}>", instId, inspectTaskNo, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }

    @Override
    public PageResult selectBompInspectTaskDetailByQueryFilter(String instId, InspectTaskDetail inspectTaskDetail, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailByQueryFilter");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class, pageInfo);
        if (null == pageResult) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>", instId);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<InspectTaskDetail> selectBompInspectTaskDetailByMchntNoOrInspectType(String instId, String mchntNo, String inspectType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInspectTaskDetailByMchntNoOrInspectType");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(mchntNo)) {
            inspectTaskDetail.setMchntNo(mchntNo);
        }
        if (!StringUtil.isEmptyOrNull(inspectType)) {
            inspectTaskDetail.setInspectType(inspectType);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号或巡检方式获取巡检任务明细信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
    }

    @Override
    public InspectTaskDetail selectBompInsepectTaskDetailByMchntNoAndStatus(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompInsepectTaskDetailByMchntNoAndStatus");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setMchntNo(mchntNo);
        /*除已巡检状态外都是未巡检，赋值已巡检状态*/
        inspectTaskDetail.setInspectStatus(InspectStatusEnum.FINISH_STATUS.getInspectStatus());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务明细信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }
}
